package com.sportizz.sport;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class DatabaseAdapter {

	static final String DATABASE_NAME = "sst_sport.db";
	static final String TABLE_NAME = "ss_player_master";
	static final String TABLE_NAME_FOLLOW = "sst_follow";
	static final String KEY_CODE = "ss_player_code";
	static final String KEY_NAME = "ss_player_name";
	static final String KEY_NATIONAL = "ss_national_code";
	static final String KEY_SPORT = "ss_sport_code";
	static final String KEY_TEAM = "ss_is_team";
	static final String KEY_Device_ID = "device_id";
	static final String KEY_P_ID = "player_id";
	static final String KEY_F_TEAM = "is_team";
	static final String KEY_F_TYPE = "follow_type";

	static final String KEY_P_player = "player_name";
	static final String KEY_F_sportcd = "sport_code";
	static final String KEY_Fsport = "sport";

	static final String KEY_F_national = "national";

	static final String CREATE_PLAYER_TABLE = "CREATE TABLE " + TABLE_NAME
			+ "(" + KEY_CODE + " TEXT," + KEY_NAME + " TEXT," + KEY_NATIONAL
			+ " TEXT," + KEY_SPORT + " TEXT," + KEY_TEAM + " TEXT" + ")";

	public SQLiteDatabase db;
	private final Context context;
	private DbHandler dbHelper;

	public DatabaseAdapter(Context _context) {
		context = _context;

	}

	@SuppressWarnings("deprecation")
	public DatabaseAdapter open() throws SQLException {
		dbHelper = new DbHandler(context);
		db = dbHelper.getWritableDatabase();
		db.setLockingEnabled(false);
		return this;
	}

	public void close() {
		dbHelper.close();
		db.close();
	}

	public SQLiteDatabase getDatabaseInstance() {
		return db;
	}

	public boolean deletetable() {
		db.execSQL("DELETE FROM " + TABLE_NAME);
		return true;
	}

	public Cursor fetchName(String inputText) throws SQLException {
		Cursor mCursor = db.query(true, TABLE_NAME, new String[] { "rowid _id",
				KEY_NAME, KEY_SPORT },
				KEY_NAME + " like '%" + inputText + "%'", null, null, null,
				null, null);
		if (mCursor != null) {
			mCursor.moveToFirst();
		}
		return mCursor;
	}

	public Cursor get_player_id(String p_Name) {

		Log.d("suraj", "DATABASE ADAPTER-" + p_Name);
		Cursor cursor_id = db.query(TABLE_NAME, new String[] { "rowid _id",
				KEY_SPORT, KEY_CODE, KEY_TEAM }, KEY_NAME + " = '" + p_Name
				+ "' limit 1", null, null, null, null, null);
		Log.d("suraj", "DATABASE-" + cursor_id);

		if (cursor_id != null) {
			cursor_id.moveToFirst();
		}

		return cursor_id;
	}

	public Cursor get_team_id(String t_Name) {

		Log.d("suraj", "DATABASE ADAPTER-" + t_Name);
		Cursor cursor_id = null;
		try {
			cursor_id = db.query(TABLE_NAME, new String[] { "rowid _id",
					KEY_CODE, KEY_TEAM },
					KEY_NAME + " like '%" + t_Name + "%'", null, null, null,
					null, null);
			if (cursor_id != null) {
				cursor_id.moveToFirst();
			}
		} catch (Exception e) {
		}
		return cursor_id;
	}

	public Long insert_follow_info(String player_id, String player_name,
			String sport_code, String sport, String national, String is_team,
			String follow_type) {

		ContentValues cv = new ContentValues();
		// cv.clear();
		cv.put("player_id", player_id);
		cv.put("player_name", player_name);
		cv.put("sport_code", sport_code);
		cv.put("sport", sport);
		cv.put("national", national);
		cv.put("is_team", is_team);
		cv.put("follow_type", follow_type);

		long c = db.insert("sst_follow", null, cv);

		Log.d("suraj", "Long = " + c);

		return c;

	}

	public Long insert_follow_info_old(String player_id, String player_name,
			String sport_code, String sport, String national, String is_team) {

		ContentValues cv = new ContentValues();
		// cv.clear();
		cv.put("player_id", player_id);
		cv.put("player_name", player_name);
		cv.put("sport_code", sport_code);
		cv.put("sport", sport);
		cv.put("national", national);
		cv.put("is_team", is_team);
		// cv.put("follow_type", follow_type);

		long c = db.insert("sst_follow", null, cv);

		Log.d("suraj", "Long = " + c);

		return c;

	}

	public int check_data(String player_code) {

		Cursor cursor_ch = db.rawQuery("select * from sst_follow where "
				+ KEY_P_ID + " = " + player_code + "", null);

		if (cursor_ch != null) {
			cursor_ch.moveToFirst();
			return cursor_ch.getCount();
		} else {
			return 0;
		}

	}

	public Cursor getfollowerlist() {
		// Cursor cur_allfollower = db.query(TABLE_NAME_FOLLOW, new String[] {
		// "rowid _id", KEY_P_ID }, KEY_F_TEAM + "='Y' OR " + KEY_F_TEAM
		// + " = 'N'", null, null, null, null, null);

		Cursor follow_list = db
				.rawQuery(
						"select player_id,sport_code from sst_follow where follow_type = 'P'",
						null);
		follow_list.moveToFirst();
		return follow_list;
	}

	public void create_sport_tab() {
		try {
			Log.d("suraj", "insert data");
			db.execSQL("delete from sport_name");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('BADMINTON','BAD','N')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('BASEBALL','BB','Y')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('BASKETBALL','BKT','Y')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('CRICKET','CRI','Y')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('FOOTBALL','SOC','Y')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('FOOTBALL USA','AFT','Y')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('FORMULA 1','F1','A')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('GOLF','GOL','A')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('HOCKEY','HOC','Y')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('ICE HOCKEY','ICE','Y')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('INDICAR','IND','A')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('MOTOGP','MGP','A')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('NASCAR','NAS','A')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('RALLY','WRC','A')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('RUGBY','RUG','Y')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('SQUASH','SQU','N')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('SUPERBIKE','SBK','A')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('TENNIS','TEN','N')");
			db.execSQL("insert into sport_name(sport,sport_code,is_team) values('UFC','UFC','N')");
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public void create_follow_tab() {
		try {
			// db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_FOLLOW +
			// "("
			// + KEY_P_ID + " TEXT," + KEY_P_player + " TEXT," + KEY_F_sportcd
			// + " TEXT," + KEY_Fsport + " TEXT," + KEY_F_national + " TEXT"
			// + "," + KEY_F_TEAM + " TEXT" + "," + KEY_F_TYPE + " TEXT" + ")");

			db.execSQL("CREATE TABLE IF NOT EXISTS t_news_language (ss_language_id TEXT,ss_news_language TEXT ,PRIMARY KEY (ss_language_id))");

			db.execSQL("CREATE TABLE IF NOT EXISTS settings (calendar_value TEXT, upcoming_value TEXT,new_lang TEXT)");

			db.execSQL("CREATE TABLE IF NOT EXISTS sportdata (sport TEXT, sport_code TEXT,team_sport TEXT)");

			Log.i("suraj", "TAble Follow Created..");
		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public Cursor getchamplist() {

		Cursor cur_champfollower = db
				.rawQuery(
						"select player_id,sport_code from sst_follow where follow_type = 'C'",
						null);
		cur_champfollower.moveToFirst();
		return cur_champfollower;

	}

	public Cursor get_p1_sport(String player_code1) {

		Cursor cur_p1_sport = db.query(TABLE_NAME, new String[] { "rowid _id",
				KEY_SPORT }, KEY_CODE + "='" + player_code1 + "'", null, null,
				null, null, null);
		if (cur_p1_sport != null) {
			cur_p1_sport.moveToFirst();
		} else {
			return null;
		}
		return cur_p1_sport;

	}

	public Cursor get_p2_sport(String player_code2) {
		Cursor cur_p2_sport = db.query(TABLE_NAME, new String[] { "rowid _id",
				KEY_SPORT }, KEY_CODE + "='" + player_code2 + "'", null, null,
				null, null, null);

		if (cur_p2_sport != null) {
			cur_p2_sport.moveToFirst();
		} else {
			return null;
		}
		return cur_p2_sport;
	}

	public Cursor getAllData_follow(String player_id) {

		Cursor getallfollow = db
				.query(TABLE_NAME_FOLLOW, new String[] { "rowid _id", KEY_P_ID,
						KEY_P_player, KEY_F_sportcd, KEY_Fsport,
						KEY_F_national, KEY_F_TEAM }, KEY_P_ID + "='"
						+ player_id + "'", null, null, null, null, null);

		if (getallfollow != null) {
			getallfollow.moveToFirst();
			return getallfollow;
		} else {
			return null;
		}
	}

	public int unfollow_player(String pcode) {

		// String
		// del_fun="delete from sst_follow where "+KEY_P_ID+" = "+pcode+"";
		// db.rawQuery(del_fun,null);

		int row = db.delete(TABLE_NAME_FOLLOW, "player_id= '" + pcode + "'",
				null);

		Log.w("suraj", "delete = " + row);
		return row;

	}

	public int total_follow() {

		// Log.d("suraj", "Dev=" + device_id);

		Cursor cur_follow_count = null;
		cur_follow_count = db.rawQuery("select * from " + TABLE_NAME_FOLLOW
				+ " where follow_type ='C' OR follow_type ='P'", null);

		cur_follow_count.moveToFirst();
		int data = cur_follow_count.getCount();

		cur_follow_count.close();
		db.close();

		if (data != 0) {

			return data;
		} else {
			return 0;
		}
	}

	public void create_notification_table() {
		String CREATE_NOTIFY_TABLE = "CREATE TABLE IF NOT EXISTS sst_notification (player1 TEXT,player2 TEXT,cs_name TEXT,date TEXT,is_team TEXT)";

		db.execSQL(CREATE_NOTIFY_TABLE);
	}

	public Cursor fetch_notify_query() {
		Cursor c = db.query("sst_notification", null, null, null, null, null,
				null);

		if (c != null) {
			c.moveToFirst();
		} else {
			return null;
		}
		return c;
	}

	public Cursor getAllfollowlist() {

		return db.rawQuery("select player_id from sst_follow", null);

	}

	public Cursor getAllfollow() {

		return db.rawQuery("select * from sst_follow", null);

	}

	public Cursor getsportDetails(String sport_name) {

		Cursor c = db.rawQuery("select * from sport_name where sport = '"
				+ sport_name + "'", null);

		if (c != null) {
			c.moveToFirst();
		} else {
			return null;
		}

		return c;

	}

	public void runScript(String sql_Script) {
		db.execSQL(sql_Script);
	}

	public Cursor tablename() {

		return db.rawQuery(
				"select name from sqlite_master where type = 'table'", null);
	}

	public void insert_lang(String lang_id, String lang_name) {

		ContentValues cv_lang = new ContentValues();
		// cv.clear();
		cv_lang.put("ss_language_id", lang_id);
		cv_lang.put("ss_news_language", lang_name);

		long c = db.insert("t_news_language", null, cv_lang);

	}

	public void insert_default_value_setting() {

		try {
			db.rawQuery("delete from settings", null);

			ContentValues cv_default_value = new ContentValues();
			// cv.clear();
			cv_default_value.put("calendar_value", "N");
			cv_default_value.put("upcoming_value", "24");
			cv_default_value.put("new_lang", "en");

			long c = db.insert("settings", null, cv_default_value);

			Log.e("suraj_", "c == " + c);

			// Cursor news_lan=db.rawQuery("select new_lang from settings",
			// null);
			// Log.e("suraj_","news_lan == "+news_lan.getColumnIndexOrThrow("new_lang"));
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public String getnews_lang() {
		Cursor news_lan = null;
		try {
			news_lan = db.rawQuery("select new_lang from settings", null);
			Log.e("suraj_", "news_lan count == " + news_lan.getCount());

			news_lan.moveToFirst();

			Log.i("suraj_",
					"news_lan == "
							+ news_lan.getString(news_lan
									.getColumnIndexOrThrow("new_lang")));

		} catch (Exception e) {
		}

		return news_lan.getString(news_lan.getColumnIndexOrThrow("new_lang"));
	}

	public String getcalValue() {

		Cursor calendar_value = null;

		calendar_value = db.rawQuery("select calendar_value from settings",
				null);

		calendar_value.moveToFirst();

		return calendar_value.getString(calendar_value
				.getColumnIndexOrThrow("calendar_value"));

	}

	public void updatecalValue(String value1, String value2) {

		 ContentValues cv=new ContentValues();
		 cv.put("calendar_value",value1);
		 
		 String where = "calendar_value= '"+ value2+"'";
		   
		 int i = db.update("settings", cv, where, null);
		   
		   Log.i("suraj","UPDATE = "+i);
		
//		db.rawQuery("update settings set calendar_value='" + value1
//				+ "' where calendar_value='" + value2 + "'", null);

	}

	public String getupcomingValue() {

		Cursor calendar_value = null;
		try {
			calendar_value = db.rawQuery("select upcoming_value from settings",
					null);
			calendar_value.moveToFirst();
		} catch (Exception e) {
			e.printStackTrace();
		}
		return calendar_value.getString(calendar_value
				.getColumnIndexOrThrow("upcoming_value"));
	}

	public void updateupcomingValue(String value1, String value2) {
		Cursor cc = db.rawQuery("update settings set upcoming_value='" + value1
				+ "' where upcoming_value='" + value2 + "'", null);

		Log.d("suraj_", "Update UPvalue =" + cc.getCount());

		Cursor calendar_value = null;
		try {
			calendar_value = db.rawQuery("select upcoming_value from settings",
					null);
			calendar_value.moveToFirst();
		} catch (Exception e) {
			e.printStackTrace();
		}
		Log.d("suraj_",
				"UPvalue ="
						+ calendar_value.getString(calendar_value
								.getColumnIndexOrThrow("upcoming_value")));

	}

	public void deletesportValue(String value1, String value2) {

		int b = db.delete("sst_follow", KEY_P_ID + "= '" + value1
				+ "' AND follow_type ='" + value2 + "'", null);

		Log.d("suraj_", "delete" + b);

	}

	public void insertSportdata(String ss_sport, String ss_sport_code,
			String ss_team_sport) {
		try {

			ContentValues cv_default_value = new ContentValues();
			// cv.clear();
			cv_default_value.put("sport", ss_sport);
			cv_default_value.put("sport_code", ss_sport_code);
			cv_default_value.put("team_sport", ss_team_sport);

			long c = db.insert("sportdata", null, cv_default_value);

			Log.e("suraj_", "long c = " + c);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Cursor getsportlist() {

		Cursor cursor = db.rawQuery("select sport from sportdata", null);
		cursor.moveToFirst();
		return cursor;
	}

	public String getsportcodelist(String sport) {

		Cursor cursor = db.rawQuery("select * from sportdata where sport = '"
				+ sport + "'", null);
		cursor.moveToFirst();

		return cursor.getString(cursor.getColumnIndexOrThrow("sport_code"));
	}

	public String getsportteamlist(String sport) {

		Cursor cursor = db.rawQuery(
				"select team_sport from sportdata where sport = '" + sport
						+ "'", null);
		cursor.moveToFirst();
		return cursor.getString(cursor.getColumnIndexOrThrow("team_sport"));
	}

	public String getsportname(String sportcode) {

		Cursor cursor = db.rawQuery(
				"select sport from sportdata where sport_code = '" + sportcode
						+ "'", null);
		cursor.moveToFirst();

		return cursor.getString(cursor.getColumnIndexOrThrow("sport"));
	}

	public Cursor getsportfollow() {

		return db.rawQuery("select * from sst_follow where follow_type = 's'",
				null);
	}

	public void deleteSportdata() {
		try {

			db.delete("sportdata", null, null);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public Cursor getfollowersport(String follow_pl_tm) {
		return db.rawQuery(
				"select sport_code from sst_follow where player_id = '"
						+ follow_pl_tm + "'", null);
	}

	public Cursor getcolumnName() {

		return db.query("sst_follow", null, null, null, null, null, null);
	}

	public boolean istablePresent() {

		Cursor cursor = db
				.rawQuery(
						"select DISTINCT tbl_name from sqlite_master where tbl_name = 'sst_follow'",
						null);
		if (cursor != null) {
			if (cursor.getCount() > 0) {
				cursor.close();
				return true;
			}
			cursor.close();
		}
		return false;
	}

	public void alter_table() {

		try {

			db.execSQL("drop table sst_follow");

			db.execSQL("CREATE TABLE " + TABLE_NAME_FOLLOW + "(" + KEY_P_ID
					+ " TEXT," + KEY_P_player + " TEXT," + KEY_F_sportcd
					+ " TEXT," + KEY_Fsport + " TEXT," + KEY_F_national
					+ " TEXT" + "," + KEY_F_TEAM + " TEXT" + "," + KEY_F_TYPE
					+ " TEXT" + ")");

		} catch (Exception e) {
			e.printStackTrace();

		}
	}

	public void create_new_follow() {

		try {
			db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_FOLLOW + "("
					+ KEY_P_ID + " TEXT," + KEY_P_player + " TEXT,"
					+ KEY_F_sportcd + " TEXT," + KEY_Fsport + " TEXT,"
					+ KEY_F_national + " TEXT" + "," + KEY_F_TEAM + " TEXT"
					+ "," + KEY_F_TYPE + " TEXT" + ")");

			Log.d("new_version", "create new follow Table");
		} catch (Exception e) {
			// TODO: handle exception
		}
	}

	public void test_old() {
		db.execSQL("CREATE TABLE IF NOT EXISTS " + TABLE_NAME_FOLLOW + "("
				+ KEY_Device_ID + " TEXT," + KEY_P_ID + " TEXT," + KEY_P_player
				+ " TEXT," + KEY_F_sportcd + " TEXT," + KEY_Fsport + " TEXT,"
				+ KEY_F_national + " TEXT" + "," + KEY_F_TEAM + " TEXT" + ")");
	}

	public Cursor getfollow() {
		// TODO Auto-generated method stub
		return null;
	}

}
